package controllers;

import helpers.PaginatorHelper;
import helpers.StringHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.ConfigSystem;

import models.AuthorModel;
import models.QuestionModel;

import entities.Author;
import entities.Category;
import entities.MetaSeo;
import entities.Question;
import entities.SiteConfig;

import admin.models.CategoryModel;
import admin.models.MetaSeoModel;

public class CategoryServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		SiteConfig config = ConfigSystem.getConfigs();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String params = path.replaceAll(request.getServletPath() + "/", "");	
		
		String id = UrlHelper.getIdFromInfoUrl(params);
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/");
			return ;
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		Category category = categoryModel.getCategory(id);
		
		if (category == null) {
			response.sendRedirect("/");
			return ;
		}
		
		int page = 1;
		try {
			page = Integer.parseInt((String) request.getParameter("page"));
		} catch (Exception e) {}
		
		boolean nextPage = false;
		boolean prevPage = false;
		int limit = 20;
		
		QuestionModel questionModel = QuestionModel.getInstance();
		List<Question> questions = questionModel.listByCategory(category.getId(), page, limit);
		
		if (questions.size() == limit) {
			nextPage = true;
		}
		
		if (page > 1) {
			prevPage = true;
		}
		
		//Get paginate
		String queryString = request.getQueryString();
		String requestURI = request.getRequestURI();
		PaginatorHelper paginator = new PaginatorHelper(queryString);
		paginator.setUrl(requestURI);
		
		List<String> aIds = new ArrayList<String>();
		List<String> cIds = new ArrayList<String>();
		
		for (int i = 0; i < questions.size(); i++) {
			aIds.add(questions.get(i).getAuthor());
			cIds.add(questions.get(i).getCategory());
		}
		
		//Get authors
		AuthorModel authorModel = AuthorModel.getInstance();
		Map<String, Author> authors = authorModel.getByListIds(aIds);
		
		//Get categories
		Map<String, Category> categories = categoryModel.getByListIds(cIds);
		
		//Set seo content
		MetaSeoModel metaseoModel = MetaSeoModel.getInstance();
		MetaSeo metaseo = metaseoModel.get("category");
		
		/*//Cache header
		response.addHeader("Cache-Control", "public");
		response.addHeader("Cache-Control", "max-age=" + 60000);*/
		
		request.setAttribute("category", category);
		request.setAttribute("questions", questions);
		request.setAttribute("authors", authors);
		request.setAttribute("categories", categories);
		request.setAttribute("paginator", paginator);
		request.setAttribute("prevPage", prevPage);
		request.setAttribute("nextPage", nextPage);
		request.setAttribute("page", page);
		request.setAttribute("metaseo", metaseo);
		try {
			request.getRequestDispatcher("/templates/" + config.getTemplate() + "/pages/category.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
}
